package com.dhnsoft.education.controller.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhnsoft.education.entity.cms.CrmBanner;
import com.dhnsoft.education.entity.cms.vo.BannerQuery;
import com.dhnsoft.education.service.cms.CrmBannerService;
import com.dhnsoft.education.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 后台banner管理接口
 * </p>
 *
 * @author 代浩楠
 * @since 2022-03-10
 */
//后台系统
@RestController
@RequestMapping("/educms/bannerAdmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    //1 分页查询banner
    @PostMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false) BannerQuery bannerQuery){
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        //构建条件
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        System.out.println("bannerQuery:"+bannerQuery);
        if (bannerQuery!=null){
            String title = bannerQuery.getTitle();
            String begin = bannerQuery.getBegin();
            String end = bannerQuery.getEnd();
            if(!StringUtils.isEmpty(title)){
                wrapper.like("title",title);
            }
            if(!StringUtils.isEmpty(begin)){
                wrapper.lt("gmt_creat",begin);
            }
            if(!StringUtils.isEmpty(end)){
                wrapper.gt("gmt_modified",end);
            }
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        bannerService.page(pageBanner,wrapper);
        return R.ok().data("rows",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    //2.添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        bannerService.save(crmBanner);
        return R.ok();
    }

    //3.根据id获取Banner
    @GetMapping("get/{id}")
    public R get(@PathVariable String id){
        CrmBanner byId = bannerService.getById(id);
        return R.ok().data("item",byId);
    }

    //更新banner
    @PutMapping("updateById")
    public R updateById(@RequestBody CrmBanner banner){
        bannerService.updateById(banner);
        return R.ok();
    }

    //删除banner
    @DeleteMapping("deleteById/{id}")
    public R deleteById(@PathVariable String id){
        bannerService.removeById(id);
        return R.ok();
    }

}

